package br.ufpe.cin.rgms.base.publicacao.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import br.com.caelum.vraptor.ioc.Component;
import br.ufpe.cin.rgms.base.publicacao.model.Publicacao;
import br.ufpe.cin.rgms.core.dao.impl.GenericDaoHibernateImpl;

@Component
public class PublicacaoDAO extends GenericDaoHibernateImpl<Publicacao, Integer> {

	@Override
	protected Class<Publicacao> getDomainClass() {
		return Publicacao.class;
	}

	@SuppressWarnings("unchecked")
	public List<Publicacao> findAllByFilter(Publicacao pub) {
		Criteria criteria = session.createCriteria(Publicacao.class);
		if(pub !=null){
			if(pub.getTitulo()!=null && !pub.getTitulo().isEmpty()){
				criteria.add(Restrictions.like("titulo", pub.getTitulo() + "%"));
			}			
			if(pub.getAno()!=null && !pub.getAno().isEmpty()){
				criteria.add(Restrictions.like("ano", pub.getAno() + "%"));
			}
			if(pub.getTipo()!=null && !pub.getTipo().isEmpty()){
				criteria.add(Restrictions.like("tipo", pub.getTipo() + "%"));
			}


		}
		return criteria.list();
	}

}
